<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Squid优化 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Squid优化</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Squid/">Squid</a>
			</span>
		
	</div>

	

	
		<h2 id="Squid优化-一"><a href="#Squid优化-一" class="headerlink" title="Squid优化(一)"></a>Squid优化(一)</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;几个SQUID重要参数：</p>
<ul>
<li>maximum_object_size 是 能cache最大的文件大小.对应wmv,rm文件,建议设置为32768 kB</li>
<li>maximum_object_size_in_memory 是在内存中cache的最大文件大小.</li>
<li>cache_mem 是SQUID可用到的最大内存.经实践,4G内存的服务器用2G；超过2G导致SQUID运行不稳</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先要分析SQUID所cache内容：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;运行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">squidclient -p 80 cache_object://localhost/info</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;能看到如下内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">Storage Swap size: 7549104 KB</div><div class="line">Storage Mem size: 418804 KB</div><div class="line">Mean Object Size: 160.46 KB</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Mean Object Size是平均内容大小,一般要把maximum_object_size_in_memory设置成离它最近的128的倍数.在这个例子中maximum_object_size_in_memory 的值应该是256kB.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cache_mem 一般设置成服务器内存的一半或更多,只要运行过程中LINUX没有使用SWAP就可以.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再就是按业务分SQUID.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;比如某个论坛,用户能上载图片和视频；当然我们要把上载的图片、视频放在单独的域名上,比如img.example.com, video.example.com；这两个域名只提供静态文件服务.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;根据统计,图片的平均大小在100KB,视频的平均大小在4M,差别是很大,应该建两个squid分别作图片和视频的CACHE.图片SQUID的 maximum_object_size_in_memory 设置为256KB,视频的SQUID的maximum_object_size_in_memory设置为8196KB.</p>
<h2 id="Squid优化-二"><a href="#Squid优化-二" class="headerlink" title="Squid优化(二)"></a>Squid优化(二)</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;探讨动态内容的CACHE.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;BBS,论坛是典型动态内容,要保证内容更新及时的同时,提高访问速度,降低数据库负担不是个简单任务.经实践发现如下办法取得很好效果：</p>
<ol>
<li>配置SQUID,对动态内容强制CACHE,用到的配置参数是refresh_pattern<br>refresh_pattern ^/forum/viewthread.php 1440 1000% 1440 ignore-reload<br>/forum/viewthread.php的内容将强制保持1天</li>
<li>修改论坛程序在用户回复帖子后,向SQUID发送PURGE命令清除相应帖子的页面CACHE,保证失效性</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;实现过这一功能,但是有时候生效,有时候无效,还未进一步查明原因.(Edit by Sean)</p>
<ol>
<li>有些频繁更新的页面可以不CACHE,用no_cache参数</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">acl no_forum_cache urlpath_regex ^/forum/forumdisplay.php</div><div class="line">no_cache DENY no_forum_cache</div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Squid/6. Squid优化/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
